<?
if($_POST['logout-draugiem-gadget'] == 1){
	setcookie('apikey', '', time()-1000);
	setcookie('apikey', '', time()-1000, '/');
}elseif($_POST['submit_apikey'] == 1){
	setcookie("apikey", $_POST['apikey']);
	?>
	<form method="POST" action="">
		<input class="red" type="submit" value="Start">
	</form>
	<?
}
?>
<html>
    <meta http-equiv=Content-Type content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="draugiem_api.css">
    <?
	if(!isset($_COOKIE['apikey'])){
	?>
		<script>
		setTimeout(' document.location=document.location' ,600000);
		</script>
    <?
	}
	?>
	<div id="draugiem-gadget-content"><?

        include("draugiem_api.php");
        $dr = new Draugiem();
        //$dr->createUrl();

        if(!isset($_COOKIE['apikey'])){
            include("register_draugiem_api.php");
            die();
        }else{
            $dr -> setApiKey($_COOKIE['apikey']);
        }

        // GET DATA
        $dr -> getData("login");
        $user = $dr -> getUser();
        $dr -> getData("counters");
        $counters = $dr -> getCounters();

        ?>
        <div style="float:left;"><?
            //VERSION
            //echo "<div style='position:relative; top:-20px; left:50px'>ver 1.0</div>";

            // DEMO
            /* $counters['unread'] = 3;
            $counters['visitors'] = 15;
            $counters['invites'] = 1;
            $counters['guestbook'] = 5;
            $counters['blogcomments'] = 10;
            $counters['gallerycomments'] = 22;
 */
            // IMAGE
            echo $user['sex'] != "M" ? "Sveika, " : "Sveiks, ";
            echo $user['name']." ".$user['surname'];

            // ONLINE
            echo $user[online] ? " <a target='_blank' href='http://www.draugiem.lv/logout.php?id=827886'>(online)</a>" : " <a target='_blank' href='http://www.draugiem.lv'>(offline)</a>";
            echo "<br/><br/>";

            // NEW MAIL
            $new_mail = ($counters['unread'] == 1)? "Jauna vēstule" : "Jaunas vēstules";
            echo strlen($counters['unread']) ? "<a target='_blank' href='http://www.draugiem.lv/messages/'>$new_mail ({$counters['unread']})</a></br>" : "";

            // VISITORS
            echo strlen($counters['visitors']) ? "<br/><a target='_blank' href='http://www.draugiem.lv/visitors/'>Apmeklējumi ({$counters['visitors']})</a><br/>" : "";

            // INVITES
            echo strlen($counters['invites']) ? "<a target='_blank' href='http://www.draugiem.lv/invite/?selectedTab=1'>Jauni ielūgumi ({$counters['invites']})</a><br/>" : "";

            // GUESTBOOK
            echo strlen($counters['guestbook']) ? "<a target='_blank' href='http://www.draugiem.lv/?selectedTab=4'>Jauni ieraksti viesugrāmatā ({$counters['guestbook']})</a><br/>" : "";

            // BLOGCOMMENTS
            echo strlen($counters['blogcomments']) ? "<a target='_blank' href='http://www.draugiem.lv/?selectedTab=3'>Jauni komentāri d-grāmatā ({$counters['blogcomments']})</a><br/>" : "";

            // GALLERYCOMMENTS
            echo strlen($counters['gallerycomments']) ? "<a target='_blank' href='http://www.draugiem.lv/?selectedTab=2'>Jauni komentāri albumā ({$counters['gallerycomments']})</a><br/>" : "";?>
        </div>

        <div style="float:right;">
            <?
            echo "<img style='' src='".$user['img']."' alt='".$user['nick']."'/><br/>";
            ?>
        </div>

        <div style="float:left;">
            <br/>
            <form method="POST" action="">
                <input class="red" type="submit" value="Refresh"><font size="1"> auto refresh 1min</font>
            </form>
            <form method="POST" action="">
                <input type="hidden" name="logout-draugiem-gadget" value="1"/>
                <input class="orange" type="submit" value="logout no draugiem gadget">
            </form>
        </div>

        <div style="float:left;">
            tavs api key: <font size="1"><?=$_COOKIE['apikey'];?></font>
        </div>

    </div>
</html>